#coding:utf-8
import time,os,logging
from str2arry import str2arry
from serialCom import serialCom
from checkSum import hex_checksum

FILE = os.getcwd()          #返回当前目录的路径
#logging基础配置，文件名为 os.path.join里面的两个值想加个，输出等级为≥DEBUG级别
logging.basicConfig(filename = os.path.join(FILE,'log.txt'),level = logging.DEBUG,
                    format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s')

def setConsumedVolume():
    print '----------------------------------------------------------'
    print 'case5 设置累积量' + '\n' + '                                    '
    logging.debug('*****case5 设置累积量*****')
    # 设置工作状态，格式
    frameHead = 'FE FE FE FE FE 68 AB 57 58'
    # meterID
    frameBody= 'F2 77 88 99 55 16 00 0B A0 16 01 00 00'
    consumedVolume=['00 00 00 12','00 00 12 12','00 00 00 00 ','99 99 99 99','09 99 99 99','00 99 99 99','00 09 99 99',]
    frameReserve='00 00'
    # 校验和
    # 16

    #读取工作状态，格式
    frameHead_Read = 'FE FE FE FE FE 68 AB 57 58'
    #ID
    frameBody_Read = 'F2 77 88 99 55 21 00 03 90 0F 01'
    #校验和
    #16

    # 读取表号
    meterIDread = (
        ''.join(('FE FE FE FE FE 68 AB 57 58 AA AA AA AA AA F2 77 88 99 55 03 00 03 81 0A 01 85 16').split()))
    ID = serialCom(meterIDread, 35)[28:38]
    ID = str2arry(ID)                                #返回值为数组
    logging.debug('获取表号 %s 用于设置参数',''.join(ID))
    # 读取表号

    for i in consumedVolume:
        frameData = frameHead.split()+ ID + frameBody.split()+ i.split() + frameReserve.split()
        frameCheckSum=hex_checksum(frameData[5:])
        frame=''.join(frameData + frameCheckSum) + '16'
        # print frame
        serialCom(frame)


        frameData_Read =frameHead_Read.split()+ID+frameBody_Read.split()
        frameCheckSum_Read=hex_checksum(frameData_Read[5:])
        frame_Read=''.join(frameData_Read+frameCheckSum_Read)+'16'
        frame_Receive=serialCom(frame_Read,51)
        # print frame_Read
        print '读取返回帧%s'%frame_Receive
        x=int(frame_Receive[56:64],16)
        consumedVolume_real=x/10+x%10/10.0
        print consumedVolume_real

        if consumedVolume_real == int(''.join(i.split()),10):
            print '累积量%s设置并读取正确'%i.replace(' ','')
            logging.debug('设置累积量%s方,读取的累计量为%0.1f,设置正确'%(i.replace(' ',''),consumedVolume_real))
        else:
            print '累积量%s设置并读取异常'%i
            logging.debug('设置累积量%s方,读取的累计量为%0.1f,设置错误'%(i.replace(' ',''),consumedVolume_real))





        #读取累计量，格式
# setConsumedVolume()
